home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWViews / FWClustr.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  6.4 KB  |  222 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWClustr.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWAROPER_H
  13. #include "FWArOper.h"
  14. #endif
  15.  
  16. #ifndef FWCLUSTR_H
  17. #include "FWClustr.h"
  18. #endif
  19.  
  20. #ifndef FWBUTTON_H
  21. #include "FWButton.h"
  22. #endif
  23.  
  24. #ifndef FWFRAME_H
  25. #include "FWFrame.h"
  26. #endif
  27.  
  28. #ifndef FWCONTXT_H
  29. #include "FWContxt.h"
  30. #endif
  31.  
  32. #ifndef FWNOTDEF_H
  33. #include "FWNotDef.h"
  34. #endif
  35.  
  36. //========================================================================================
  37. // File scope definitions
  38. //========================================================================================
  39.  
  40. #ifdef FW_BUILD_MAC
  41. #pragma segment fwgadgts
  42. #endif
  43.  
  44. //========================================================================================
  45. //    CLASS FW_CRadioCluster
  46. //========================================================================================
  47.  
  48. FW_DEFINE_AUTO(FW_CRadioCluster)
  49. FW_DEFINE_CLASS_M0(FW_CRadioCluster)
  50.  
  51. // This class is archivable, but we provide the archiving implementation in a separate
  52. // translation unit in order to enable deadstripping of the archiving-related code
  53. // in parts that do not use archiving with this class.
  54.  
  55. //----------------------------------------------------------------------------------------
  56. // FW_CRadioCluster::FW_CRadioCluster
  57. //----------------------------------------------------------------------------------------
  58.  
  59. FW_CRadioCluster::FW_CRadioCluster(Environment* ev) :
  60.     FW_MReceiver(),
  61.     fButtonList(),
  62.     fButtonOn(0)
  63. {
  64. FW_UNUSED(ev);
  65.     FW_END_CONSTRUCTOR
  66. }
  67.  
  68. //----------------------------------------------------------------------------------------
  69. // FW_CRadioCluster::~FW_CRadioCluster
  70. //----------------------------------------------------------------------------------------
  71.  
  72. FW_CRadioCluster::~FW_CRadioCluster()
  73. {
  74.     FW_START_DESTRUCTOR
  75. }
  76.  
  77. //----------------------------------------------------------------------------------------
  78. // FW_CRadioCluster::AddRadio
  79. //----------------------------------------------------------------------------------------
  80.  
  81. void FW_CRadioCluster::AddRadio(Environment* ev, FW_CButton* radio)
  82. {
  83.     FW_ASSERT(radio != NULL && radio->GetButtonKind(ev) == FW_kRadioButton);
  84.  
  85.     // This works only if the radio button uses the default button message
  86.     // Otherwise the RadioCluster will not catch the notification
  87.     AddInterest(FW_CInterest(radio, FW_kRadioClusterMsg));
  88.     fButtonList.AddLast(radio);
  89.     
  90.     // Use the first button added to the cluster as the default one
  91.     if (!fButtonOn)
  92.     {
  93.         fButtonOn = radio;
  94.         radio->SetValue(ev, 1);
  95.     }
  96.     else if (radio->GetValue(ev) != 0)
  97.     {
  98.         fButtonOn->SetValue(ev, 0);
  99.         fButtonOn = radio;
  100.     }
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // FW_CRadioCluster::RemoveRadio
  105. //----------------------------------------------------------------------------------------
  106.  
  107. void FW_CRadioCluster::RemoveRadio(Environment* ev, FW_CButton* radio)
  108. {
  109. FW_UNUSED(ev);
  110.     // It is dangerous to delete the selected radio button
  111.     FW_ASSERT(fButtonOn != radio);
  112.     
  113.     RemoveInterest(FW_CInterest(radio, FW_kRadioClusterMsg));
  114.     fButtonList.Remove(radio);
  115. }
  116.  
  117. //----------------------------------------------------------------------------------------
  118. // FW_CRadioCluster::HandleNotification
  119. //----------------------------------------------------------------------------------------
  120.  
  121. void FW_CRadioCluster::HandleNotification(Environment* ev, const FW_CNotification& notification)
  122. {
  123.     switch (notification.GetMessage())
  124.     {
  125.         case FW_kRadioClusterMsg:
  126.         {
  127.             const FW_CControlNotification& controlNotification = 
  128.                                                     (FW_CControlNotification&) notification;
  129.             FW_CButton* radioClicked = FW_DYNAMIC_CAST(FW_CButton, controlNotification.GetControl(ev));
  130.             FW_ASSERT(radioClicked);
  131.             FW_TOrderedCollectionIterator<FW_CButton> iter(&fButtonList);
  132.             for (FW_CButton* button = iter.First(); iter.IsNotComplete(); button = iter.Next())
  133.             {
  134.                 if (button != radioClicked && button->GetValue(ev) == 1)
  135.                     button->SetValue(ev, 0);
  136.             }
  137.             
  138.             fButtonOn = radioClicked;
  139.         }
  140.         break;
  141.         
  142.         case FW_kNotifierDeletedMsg:
  143.             FW_CButton* button = FW_DYNAMIC_CAST(FW_CButton, notification.GetNotifier());
  144.             if (button && fButtonList.Contains(button))
  145.             {
  146.                 fButtonList.Remove(button);
  147.                 if (fButtonList.Count() == 0)
  148.                     delete this;
  149.             }
  150.             break;    
  151.         
  152.         default:
  153.             FW_DEBUG_MESSAGE("FW_CRadioCluster can't handle this message");
  154.             break;
  155.     }
  156. }
  157.  
  158. //----------------------------------------------------------------------------------------
  159. //    FW_CRadioCluster::Flatten
  160. //----------------------------------------------------------------------------------------
  161.  
  162. void FW_CRadioCluster::Flatten(Environment* ev, FW_CWritableStream& stream) const
  163. {
  164.     // If all radio buttons have been removed at runtime the radio-cluster was deleted too
  165.     
  166.     FW_CSuperView* superView = fButtonOn->GetSuperView(ev);
  167.     FW_WRITE_DYNAMIC_OBJECT(stream, superView, FW_CSuperView);
  168.  
  169.     // [LSD] todo: add code to retrieve the list of radio buttons connected    
  170.     short numRadioButtons = 0;  
  171.     long dummy = 0;
  172.     
  173.     stream << dummy, numRadioButtons;
  174. /*
  175.     FW_TOrderedCollectionIterator<FW_CInterest> ite(&fInterestList);
  176.     short numRadioButtons = ite.Count();  
  177.     long dummy = 0;
  178.     
  179.     stream << dummy, numRadioButtons;
  180.  
  181.     FW_CNotifier* nofifier;
  182.     FW_CButton* button;
  183.     for (anInterest = ite.First(); ite.IsNotComplete(); anInterest = ite.Next())
  184.     {
  185.         notifier = anInterest->GetNotifier();
  186.         button = FW_DYNAMIC_CAST(FW_CButton, notifier);
  187.         FW_ASSERT(button != 0);
  188.         
  189.         stream << button->GetViewID(ev);    
  190. */
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. //    FW_CRadioCluster::InitializeFromStream
  195. //----------------------------------------------------------------------------------------
  196.  
  197. void FW_CRadioCluster::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  198. {
  199.     // Read the parent view of the radio buttons 
  200.     FW_CSuperView* parentView;    
  201.     FW_READ_DYNAMIC_OBJECT(stream, &parentView, FW_CSuperView);
  202.     
  203.     short numRadioButtons;
  204.     stream >> numRadioButtons;
  205.     
  206.     FW_ASSERT(numRadioButtons > 0);
  207.     
  208.     for (short i = 1; i <= numRadioButtons; i++)
  209.     {
  210.         ODID    radioID;
  211.         stream >> radioID;
  212.         
  213.         FW_CView* view = parentView->FindViewByID(ev, radioID);
  214.         FW_CButton* button = FW_DYNAMIC_CAST(FW_CButton, view);
  215.         FW_ASSERT(button != NULL);
  216.         
  217.         AddRadio(ev, button);
  218.     }    
  219. }
  220.  
  221.  
  222.